package com.moli.iov.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.manager.mapper.AuthorityAuthMapper;
import com.moli.iov.manager.model.po.AuthorityAuth;
import com.moli.iov.manager.model.po.AuthoritySysUser;
import com.moli.iov.manager.service.IAuthorityAuthService;
import com.moli.iov.manager.service.IAuthorityRoleAuthService;
import com.moli.iov.manager.service.IAuthoritySysUserService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限菜单表 服务实现类
 * </p>
 *
 * @author fudaohui
 * @since 2018-12-12
 */
@Slf4j
@Service("iAuthorityAuthService")
public class AuthorityAuthServiceImpl extends ServiceImpl<AuthorityAuthMapper, AuthorityAuth> implements IAuthorityAuthService {

    @Autowired
    private AuthorityAuthMapper authorityAuthMapper;
    @Autowired
    private IAuthoritySysUserService iAuthoritySysUserService;

    @Override
    public List<AuthorityAuth> queryChildAuthsByPid(long pid) {

        QueryWrapper<AuthorityAuth> queryWrapper = new QueryWrapper();
        queryWrapper.select("id", "auth_name", "pid", "path_uri", "forward_uri", "is_menu");
        queryWrapper.eq("pid", pid);
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByAsc("sort");
        return list(queryWrapper);
    }

    @Override
    public List<AuthorityAuth> queryChildAuthsByPidAndRoleId(Long roleId, Integer isMemu, Long pid) {

        List<AuthorityAuth> authorityAuths = new ArrayList<>();
        List<AuthorityAuth> child = queryAuthByRoleId(roleId, isMemu, pid);
        for (AuthorityAuth authorityAuth : child) {
            List<AuthorityAuth> authorityAuths1 = queryChildAuthsByPidAndRoleId(roleId,0,authorityAuth.getId());
            authorityAuth.setChildren(authorityAuths1);
            authorityAuths.add(authorityAuth);

        }
        return authorityAuths;
    }

    @Override
    public List<AuthorityAuth> queryChildAuth(long pid) {
        List<AuthorityAuth> authorityAuths = new ArrayList<>();
        List<AuthorityAuth> childAuthsByPid = queryChildAuthsByPid(pid);
        for (AuthorityAuth authorityAuth : childAuthsByPid) {
            List<AuthorityAuth> child = queryChildAuth(authorityAuth.getId());
            authorityAuth.setChildren(child);
            authorityAuths.add(authorityAuth);
        }
        return authorityAuths;
    }

    @Override
    public List<AuthorityAuth> queryAuthTreeByRoleId(long roleId, int isMemu) {
        AuthorityAuth authorityAuth1 = new AuthorityAuth();
        List<AuthorityAuth> authorityAuths = new ArrayList<>();
        List<AuthorityAuth> fistLevelAuths = queryAuthByRoleId(roleId, isMemu, null);
        for (AuthorityAuth fistLevelAuth : fistLevelAuths) {
            List<AuthorityAuth> authorityAuths1 = queryChildAuthsByPidAndRoleId(roleId, isMemu, fistLevelAuth.getId());
            fistLevelAuth.setChildren(authorityAuths1);
            authorityAuths.add(fistLevelAuth);
            String authName = fistLevelAuth.getAuthName();
            if (authName != null && authName.contains("报警管理")) {
                authorityAuth1.setAuthName("首页报警");
                authorityAuth1.setId(-1L);
                authorityAuths.add(authorityAuth1);
            }
        }
        return authorityAuths;
    }

    @Override
    public List<AuthorityAuth> queryAuthTreeByUserId(long userId, int isMemu) {

        Long roleId = getRoleIdFromUserId(userId);
        List<AuthorityAuth> authorityAuths = queryAuthTreeByRoleId(roleId, isMemu);
        return authorityAuths;
    }

    Long getRoleIdFromUserId(long userId) {
        RestResponse restResponse = iAuthoritySysUserService.queryUserById(userId);
        long roleId = 0;
        if (restResponse != null && restResponse.getCode() == 0) {
            AuthoritySysUser authoritySysUser = (AuthoritySysUser) restResponse.getData();
            if (authoritySysUser != null) {
                roleId = authoritySysUser.getRoleId();
            }
        }
        return roleId;
    }

    @Override
    public List<AuthorityAuth> queryAuthByUserId(Long userId) {
        long roleId = getRoleIdFromUserId(userId);
        List<AuthorityAuth> authorityAuths = authorityAuthMapper.queryAllAuthByRoleId(roleId);
        return authorityAuths;
    }

    @Override
    public List<AuthorityAuth> queryAuthByRoleId(Long roleid, Integer isMemu, Long flag) {
        return authorityAuthMapper.queryAuthByUserId(roleid, isMemu, flag);
    }

    @Override
    public List<AuthorityAuth> queryAuthByUri(String uri) {
        QueryWrapper<AuthorityAuth> authQueryWrapper = new QueryWrapper<>();
        authQueryWrapper.eq("is_menu", 2);
        authQueryWrapper.eq("is_delete", 0);
        authQueryWrapper.eq("path_uri", uri);
        return list(authQueryWrapper);
    }

    @Override
    public List<AuthorityAuth> queryAllAuths() {
        List<AuthorityAuth> authorityAuths = new ArrayList<>();
        QueryWrapper<AuthorityAuth> authQueryWrapper = new QueryWrapper<>();
        authQueryWrapper.isNull("pid");
        authQueryWrapper.eq("is_delete", 0);
        authQueryWrapper.eq("is_menu", 1);
        authQueryWrapper.orderByAsc("sort");
        List<AuthorityAuth> fistLevelAuths = list(authQueryWrapper);
        for (AuthorityAuth fistLevelAuth : fistLevelAuths) {
            List<AuthorityAuth> child = queryChildAuth(fistLevelAuth.getId());
            fistLevelAuth.setChildren(child);
            authorityAuths.add(fistLevelAuth);
        }
        return authorityAuths;
    }
}
